﻿/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;
using IAPRepo_WebRole.Models;
using System.Diagnostics;

namespace IAPRepo_WebRole.Controllers
{
    public abstract class BaseCloudStorageController : Controller
    {
        protected static CloudStorageAccount account;
        protected static TableServiceContext tableContext;
        protected static CloudBlobContainer blobContainer;

        static BaseCloudStorageController()
        {
            Debug.WriteLine("First instance of BaseCloudStorageController created");            
            account = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(WebRole.DATA_CONNECTION_STRING));

            tableContext = account.CreateCloudTableClient().GetDataServiceContext();
            blobContainer = account.CreateCloudBlobClient().GetContainerReference(IAPProduct.BLOB_CONTAINER_NAME);
        }

        protected IAPProduct getProduct(string rowKey)
        {
            return
                tableContext.CreateQuery<IAPProduct>(IAPProduct.TABLE_NAME)
                .Where(p => p.PartitionKey == IAPProduct.PARTITION_KEY && p.RowKey == rowKey).Single();
        }
    }
}
